package org.fin.homeworkspringboot.controller;

import org.fin.homeworkspringboot.pojo.Article;
import org.fin.homeworkspringboot.pojo.vo.PageModel;
import org.fin.homeworkspringboot.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
public class PageController {

  private final ArticleService articleService;

  @Autowired
  public PageController(ArticleService articleService) {
    this.articleService = articleService;
  }

  @GetMapping("/index")
  public ModelAndView index(ModelAndView mv, @PageableDefault(size = 2) Pageable pageable) {
    Page<Article> page = articleService.findAllByPage(pageable);

    // 构建页码model对象并设置到"page"里
    mv.addObject("page", PageModel.of(page));
    // 将查询结果设置到"page"里
    mv.addObject("result", page.getContent());
    // 设置逻辑视图名
    mv.setViewName("client/index");
    return mv;
  }
}
